﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text.RegularExpressions;
using System.Data;
using System.IO;
using System.Text;

namespace aucn.web.au
{
    public partial class subject : System.Web.UI.Page
    {
        XmlDocument doc;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Master.Attributes["submenu"] = "Undergrad";

            if (Request.QueryString["d"] != null)
            {
                string id = Request.QueryString["d"].Trim().ToUpper();
                if (Cache["DEPT_" + id] == null)
                {
                    doc = new XmlDocument();


                    doc.Load(Server.MapPath("~/App_Data/subjects.xml"));
                    XmlNamespaceManager xm = new XmlNamespaceManager(doc.NameTable);
                    xm.AddNamespace("ab", "http://tempuri.org/subject.xsd");

                    XmlNode deptNode = doc.DocumentElement.SelectSingleNode("ab:department[@name='" + id + "']", xm);
                    if (deptNode != null)
                    {


                        Department dpt = new Department(
                            id,
                            deptNode.SelectSingleNode("ab:name", xm).InnerText,
                            deptNode.SelectSingleNode("ab:chname", xm).InnerText,
                            deptNode.SelectSingleNode("ab:desc", xm) != null ? deptNode.SelectSingleNode("ab:desc", xm).InnerText : string.Empty,
                            deptNode.SelectSingleNode("ab:career", xm) != null ? deptNode.SelectSingleNode("ab:career", xm).InnerText : string.Empty,
                            deptNode.SelectSingleNode("ab:common", xm) != null ? deptNode.SelectSingleNode("ab:common", xm).InnerText : string.Empty,
                            new List<Major>()
                            );

                        XmlNodeList majorNodes = deptNode.SelectNodes("ab:majors/ab:major", xm);

                        foreach (XmlNode majorNode in majorNodes)
                        {
                            dpt.MajorList.Add(new Major(majorNode.Attributes["name"].Value,
                                majorNode.SelectSingleNode("ab:name", xm) != null ? majorNode.SelectSingleNode("ab:name", xm).InnerText : string.Empty,
                                majorNode.SelectSingleNode("ab:desc", xm) != null ? majorNode.SelectSingleNode("ab:desc", xm).InnerText : string.Empty,
                                majorNode.SelectSingleNode("ab:career", xm) != null ? majorNode.SelectSingleNode("ab:career", xm).InnerText : string.Empty,
                                majorNode.SelectSingleNode("ab:target", xm) != null ? majorNode.SelectSingleNode("ab:target", xm).InnerText : string.Empty,
                                majorNode.SelectSingleNode("ab:subjects", xm) != null ? majorNode.SelectSingleNode("ab:subjects", xm).InnerText : string.Empty,
                                majorNode.Attributes["new"] != null ? Convert.ToBoolean(int.Parse(majorNode.Attributes["new"].Value)) : false
                                ));
                            
                        }

                        Cache["DEPT_" + id] = dpt;


                        //this.BindDeptData(deptName);

                    }
                    else
                    {
                        this.Title = "Error!";
                        return;
                    }
                }

                this.BindDeptData(id);
            }
            else
            {
                this.Title = "Error!";
                return;
            }
        }

        protected void BindDeptData(string deptId)
        {
            Department dept = (Department)Cache["DEPT_" + deptId];
            this.Title = string.Format("{1} {0}", dept.DeptName, dept.DeptChName);
            ltrDeptDesc.Text = dept.Description;

            //string dname = dept.DeptChName.Substring(dept.DeptChName.IndexOf(' ') + 1);

            ltrDeptName.Text = string.Format("{1}<br/>{0}", dept.DeptName, dept.DeptChName);

            if (dept.CommonSubjects != string.Empty)
            {
                string t = Regex.Replace(dept.CommonSubjects, @"\r\n", "</td></tr><tr><td>");
                t = Regex.Replace(t, @"\t{2,}", "</td><td class='last'>");
                t = Regex.Replace(t, "(First|Second|Third|Fourth) (Semester|Year)", "<em>$1 $2</em>");
                t = Regex.Replace(t, "第(一|二|三|四)(学期|学年)", "<em>第$1$2</em>");
                ltrCommonSubjects.Text = "<table class='subject'>" + t + "</table>";
            }
            else
            {
                pnCommon.Visible = false;
            }

            // get majors
            StringBuilder mhtml = new StringBuilder();
            mhtml.Append("<ul>");
            foreach (Major m in dept.MajorList)
            {
                mhtml.AppendFormat("<li><a href='major.aspx?d={0}&m={1}'>{2}</a> {3}</li>", Request.QueryString["d"], m.MajorName, m.MajorChName, m.IsNew?string.Format("<img src='{0}' />", (String)GetGlobalResourceObject("Resource1", "NewIconPathAbs")):"");
            }
            mhtml.Append("</ul>");

            ltrMajors.Text = mhtml.ToString();
        }
    }
}

